const debounce = (func, wait, immediate) => {
  let timeout, args, context, timestamp, result;
  const later = function () {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp;
    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args);
        if (!timeout) context = args = null;
      }
    }
  };

  return function (...args) {
    context = this;
    timestamp = +new Date();
    const callNow = immediate && !timeout;
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait);
    if (callNow) {
      result = func.apply(context, args);
      context = args = null;
    }
    return result;
  };
};

/* 时间格式处理 */
const timeFormat = (date, status) => {
  let time = new Date(date)
  const y = time.getFullYear(); //年
  const m = (time.getMonth() + 1).toString().padStart(2, '0'); //月
  const d = time.getDate().toString().padStart(2, '0'); //日
  if (status) return `${y}-${m}-${d} 00:00:00`;
  return `${y}-${m}-${d} 23:59:59`;
}

/* 时间减一天 */
const getTimes = (time) => {
  time = new Date(time.getTime() - 3600 * 1000 * 24 * 1);
  const year = time.getFullYear();
  const month = (time.getMonth() + 1).toString().padStart(2, "0");
  const day = time.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day} 23:59:59`;
}
/* 时间减一周 */
const getTimeWeek = (time) => {
  time = new Date(time.getTime() - 3600 * 1000 * 24 * 7);
  const year = time.getFullYear();
  const month = (time.getMonth() + 1).toString().padStart(2, "0");
  const day = time.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day} 23:59:59`;
}
/* 时间减一月 */
const getTimeMonth = (times) => {
  let time = new Date(times)
  time = new Date(time.getTime() - 3600 * 1000 * 24 * 30);
  const year = time.getFullYear();
  const month = (time.getMonth() + 1).toString().padStart(2, "0");
  const day = time.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day} 23:59:59`;
}

/* ------------------------------------------------------ */
/* 本周星期一 */
const getFirstDayOfWeek = (date, status = true) => {
  const weekday = date.getDay() || 7;
  date.setDate(date.getDate() - weekday + 1);
  if (status) return timeFormat(date, status);
  return getWeekLast(date);
}

/* 本周最后一天 */
const getWeekLast = (data) => {
  const nowTime = data.getTime();
  const day = data.getDay();
  const oneDayTime = 24 * 60 * 60 * 1000;
  const SundayTime = nowTime + (7 - day) * oneDayTime;
  return timeFormat(new Date(SundayTime));
}

/* ------------------------------------------------------ */
/* 本月第一天 */
const getFirstDayOfMonth = (date, status = true) => {
  date.setDate(1);
  if (status) return timeFormat(date, status, '月');
  return CurrentMonthLast(date);
}
/* 本月最后一天 */
const CurrentMonthLast = (date) => {
  let currentMonth = date.getMonth();
  let nextMonth = ++currentMonth;
  let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  let oneDay = 1000 * 60 * 60 * 24;
  return timeFormat(new Date(nextMonthFirstDay - oneDay));
}

/* ------------------------------------------------------ */
/* 本年年初 */
const getFirstDayOfYear = (date, status = true) => {
  date.setDate(1);
  date.setMonth(0);
  if (status) return timeFormat(date, status, '年');
  return getEndYear(date);
}
/* 本年年尾 */
const getEndYear = (date) => {
  date.setFullYear(date.getFullYear() + 1);
  date.setMonth(0);
  date.setDate(0);
  return timeFormat(date);
}

/* 随机颜色 */
const getRandomColor = () => {
  return (
    "#" +
    parseInt(0x1000000 * Math.random())
      .toString(16)
      .padStart(6, "0")
  );
}

/* 判断对象是否为空 */
const isObj = (obj) => {
  for (let i in obj) {
    return true
  }
  return false
}
/* 删除对象空值 */
const setObjNull = (obj) => {
  let obj1 = JSON.parse(JSON.stringify(obj))
  for (let key in obj1) {
    if (obj1[key] == '-') delete obj1[key]
  }
  return obj1
}

/* ****************地图相关******************* */
/* 弹窗模板 */
const getTemplate = (datas, that) => {
  return `
  <div
  class="minPopup"
>
  <header class="flex">
    <h4 class="titles fs-18 familyE">${datas.name}</h4>
  </header>
  <!-- <div id="close">
    <i class="el-icon-close cr-main fs-24 bold"></i>
  </div> -->
  <div class="content">
    <div class="fs-12 bold flex-start mb-12">
      <div class="flex_s">
        <span class="cr-hui title-width">点位地址：</span>
        <span class="cr-white"
          >${datas.area}</span
        >
      </div>
    </div>
    <div class="fs-12 bold flex-start mb-12">
      <div class="flex_s">
        <div class="item-width">
          <span class="cr-hui title-width">联系人：</span>
          <span class="cr-white">${datas.contactPerson}</span>
        </div>
        <div class="item-width">
          <span class="cr-hui title-width">联系电话：</span>
          <span class="cr-white">${datas.contactNumber}</span>
        </div>
      </div>
    </div>
    <div class="fs-12 bold flex-start mb-12">
      <div class="flex_s">
        <div class="item-width">
          <span class="cr-hui title-width">建筑面积：</span>
          <span class="cr-white">${datas.measure} ㎡</span>
        </div>
        <div class="item-width">
          <span class="cr-hui title-width">建筑高度：</span>
          <span class="cr-white">${datas.height}m</span>
        </div>
      </div>
    </div>
    <div class="fs-12 bold flex-start mb-12">
      <div class="flex_s">
        <span class="cr-hui title-width">累计碳排放：</span>
        <span class="cr-white">${datas.emissions}kgCO₂</span>
      </div>
    </div>
    <div class="fs-12 bold flex-start mb-12">
      <div class="flex_s">
        <div class="item-width">
          <span class="cr-hui title-width">累计用电量：</span>
          <span class="cr-white">${datas.electric}KW.h</span>
        </div>
        <div class="item-width">
          <span class="cr-hui title-width">累计用水量：</span>
          <span class="cr-white">${datas.water}t</span>
        </div>
      </div>
    </div>
    <div class="fs-12 bold flex mb-10">
      <div class="cr-white">
        <span>监测数据：</span>
        <span>${datas.gmtCreate}</span>
      </div>
       <div class="cr-huang info" @click="detailsAction">历史数据</div>
    </div>
    <div class="fs-12 bold flex_end mb-10">
      <div class="item_box mb-16" v-for="(item,index) in factor" :key="index">
        <div class="fs-12 flex">
          <div class="cr-white">{{item.name}}</div>
          <div class="symbol cr-hui1">
            <div>{{item.symbol}}</div>
            <div class="line-s"></div>
          </div>
        </div>
        <div class="flex mt-6">
          <span class="fs-16 familyD cr-white">{{item.number}}</span>
          <span class="cr-hui1 fs-12 mr-12">{{item.amount}}</span>
        </div>
      </div>
    </div>
  </div>
</div>
  `
}
/* 获取点位颜色 */
// const getStatus = (status, type) => {
//   const itemArr = {
//     建筑面积: '',
//     建筑高度: 's',
//     建筑分类: 's1',
//   }
//   const colorType = {
//     lv11: {
//       color1: `lv11${itemArr[type]}`,
//       color2: "lv12",
//       color3: "lv13",
//     },
//     lv21: {
//       color1: `lv21${itemArr[type]}`,
//       color2: "lv22",
//       color3: "lv23",
//     },
//     lv31: {
//       color1: `lv31${itemArr[type]}`,
//       color2: "lv32",
//       color3: "lv33",
//     },
//     lv41: {
//       color1: `lv41${itemArr[type]}`,
//       color2: "lv42",
//       color3: "lv43",
//     },
//   };

//   switch (type) {
//     case "建筑面积":
//       if (status <= 500) return colorType['lv11']
//       if (status > 500 && status < 1000) return colorType['lv21']
//       if (status > 1000 && status < 1500) return colorType['lv31']
//       if (status > 1500) return colorType['lv41']
//       break;
//     case "建筑高度":
//       if (status <= 10) return colorType['lv11']
//       if (status > 10 && status < 20) return colorType['lv21']
//       if (status > 20 && status < 30) return colorType['lv31']
//       if (status > 30) return colorType['lv41']
//       break;
//     case "建筑分类":
//       if (status === 0) return colorType['lv11']
//       if (status === 1) return colorType['lv21']
//       if (status === 2) return colorType['lv31']
//       if (status === 3) return colorType['lv41']
//       break;
//   }
// }

/* 自定义地图 */
const setMapInfo = (data, info, type) => {
  const arrs = {
    建筑面积: "markerIcon",
    建筑高度: "markerIcons",
    建筑分类: "markerIcon3",
  }
  const { contactPerson, contactNumber } = info
  return `
    <div class="Point_wrap">
      <div class="${arrs[type]} ${data.color1}">
        <div class="${data.color2}">
          <div class="${data.color3}"></div>
        </div>
      </div>
      <div class="min-Tips">
        <a title="${contactPerson}">联系人：${contactPerson}</a>
        <a>电话：${contactNumber}</a>
      </div>
    </div>
    `;
}
const fullScreen = () => {
  let el = document.documentElement;
  let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;
  if (typeof rfs != "undefined" && rfs) {
    rfs.call(el);
  };
  return;
}
const exitScreen = () => {
  if (document.exitFullscreen) {
    document.exitFullscreen();
  }
  else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  }
  else if (document.webkitCancelFullScreen) {
    document.webkitCancelFullScreen();
  }
  else if (document.msExitFullscreen) {
    document.msExitFullscreen();
  }
  if (typeof cfs != "undefined" && cfs) {
    cfs.call(el);
  }
}
const isFullScreen = () => {
  return !!(
    document.fullscreen ||
    document.mozFullScreen ||
    document.webkitIsFullScreen ||
    document.webkitFullScreen ||
    document.msFullScreen
  );
}

export {
  isFullScreen,
  exitScreen,
  fullScreen,
  debounce,
  getFirstDayOfWeek,
  getFirstDayOfMonth,
  getFirstDayOfYear,
  getRandomColor,
  CurrentMonthLast,
  getWeekLast,
  getTimes,
  getEndYear,
  isObj,
  getTemplate,
  setMapInfo,
  timeFormat,
  getTimeWeek,
  getTimeMonth,
  setObjNull,
}